package com.qf.common.baidu.utils;

import com.baidu.aip.contentcensor.AipContentCensor;
import com.qf.common.baidu.info.CheckResult;
import org.json.JSONArray;
import org.json.JSONObject;

/**
 * 内容审核工具类
 */
public class BaiduTextCheckUtils {

    // 查看应用的appid等信息，进行填充
    public static final String APP_ID = "119069669";
    public static final String API_KEY = "4lldZEasCaD6SDoQXiyr1qf5";
    public static final String SECRET_KEY = "qtmMV5FX7R8XHuQmDCdSFhcgx5poZlA6";

    private static AipContentCensor client;

    static {
        client = new AipContentCensor(APP_ID, API_KEY, SECRET_KEY);
        // 可选：设置网络连接参数
        client.setConnectionTimeoutInMillis(2000);
        client.setSocketTimeoutInMillis(60000);
    }

    /**
     * 内容审核
     *
     * @param text
     */
    public static CheckResult textCheck(String text) {
        JSONObject response = client.textCensorUserDefined(text);

        System.out.println(response.toString());
        // 是否合规的状态，1.合规，2.不合规，3.疑似，4.审核失败
        int conclusionType = response.getInt("conclusionType");

        CheckResult checkResult = new CheckResult();
        checkResult.setConclusionType(conclusionType);

        System.out.println(conclusionType);
        // 获取不合规的数据
        if (conclusionType != 1) {
            String msg = "";
            JSONArray data = response.getJSONArray("data");
            for (int i = 0; i < data.length(); i++) {
                JSONObject jsonObject = data.getJSONObject(i);
                System.out.println(jsonObject.getString("msg"));
                msg += (jsonObject.getString("msg") + ";");
            }
            checkResult.setMsg(msg);
        }
        return checkResult;
    }

    public static void main(String[] args) {
        BaiduTextCheckUtils.textCheck("欢迎使用产品，他妈的");
    }

}
